using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO
{
    public class GalleryImageDAO : Abstracts.AbstractGenericDAO<GalleryImage, System.Nullable<int>>,
        Interfaces.IGalleryImageDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "gallery_image_id";
        }

        /// <summary>
        /// Pobieranie listy obiektow na podstawie ich galerii i strony.
        /// </summary>
        /// <param name="galleryId">galeria</param>
        /// <param name="pageId">strona</param>
        /// <returns>lista rekordow o zadanej galerii i stronie</returns>
        public IQueryable<GalleryImage> FindByGalleryAndPage(int galleryId, int pageId)
        {
            if (galleryId.Equals(0) || pageId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<GalleryImage> gallerie_images = this._cmsEntitiesDataContext.GalleryImages.Where(
                    gall_page => gall_page.gallery_id.Equals(galleryId) && gall_page.page_id.Equals(pageId)
                        );

                if (!(gallerie_images.Any()))
                {
                    return null;
                }   //if
                return gallerie_images;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie listy obiektow na podstawie ich nazwy galerii i strony.
        /// </summary>
        /// <param name="galleryName">nazwa galerii</param>
        /// <param name="pageId">strona</param>
        /// <returns>lista rekordow o zadanym języku i szablonie</returns>
        public IQueryable<GalleryImage> FindByGalleryNameAndPage(String galleryName, int pageId)
        {
            if (String.IsNullOrEmpty(galleryName) || pageId.Equals(0))
            {
                return null;
            }   //if
            try
            {


                IQueryable<GalleryImage> gallerie_images = this._cmsEntitiesDataContext.GalleryImages.Where(
                    gaim => gaim.page_id.Equals(pageId) && gaim.gallery_id.HasValue &&
                    this._cmsEntitiesDataContext.Galleries.Where(
                        gall => gall.gallery_name.Equals(galleryName)
                            ).Select(ga => ga.gallery_id).Contains(gaim.gallery_id.Value)
                             );

                if (!(gallerie_images.Any()))
                {
                    return null;
                }   //if
                return gallerie_images;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie listy obiektow na podstawie ich języka.
        /// </summary>
        /// <param name="languageId">język</param>
        /// <returns>lista rekordow o zadanym języku</returns>
        public IQueryable<GalleryImage> FindByLanguage(int languageId)
        {
            if (languageId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<GalleryImage> galleryImages = this._cmsEntitiesDataContext.GalleryImages.Where(
                    gaim => gaim.gallery_id.HasValue &&
                    this._cmsEntitiesDataContext.Galleries.Where(
                        gall => gall.language_id.Equals(languageId)
                    ).Select(ga => ga.gallery_id).Contains(gaim.gallery_id.Value)
                ); 
                if (!(galleryImages.Any()))
                {
                    return null;
                }   //if
                return galleryImages;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Wstawianie listy obiektów na podstawie jezyka.
        /// </summary>
        /// <param name="defaultLanguageId">domyslny jezyk</param>
        /// <param name="newLanguageId">nowy jezyk</param>
        /// <returns>true jesli sie udalo, false wpp</returns>
        public bool InsertForNewLanguage(int defaultLanguageId, int newLanguageId)
        {
            if (defaultLanguageId.Equals(0) || newLanguageId.Equals(0))
            {
                return false;
            }   //if
            try
            {
                IQueryable<GalleryImage> galleryImages = this.FindByLanguage(defaultLanguageId);
                GalleryDAO _galleryDAO = new GalleryDAO();
                PageDAO _pageDAO = new PageDAO();
                if (galleryImages == null)
                {
                    return true;
                }   //if
                if (!(galleryImages.Any()))
                {
                    return true;
                }   //if
                foreach (GalleryImage gaim in galleryImages)
                {
                    Gallery oldGallery = _galleryDAO.FindByPK(gaim.gallery_id);
                    if (oldGallery == null)
                    {
                        return false;
                    }
                    Gallery newGallery = _galleryDAO.FindByNameAndLanguageId(oldGallery.gallery_name, newLanguageId);
                    if (newGallery == null)
                    {
                        return false;
                    }
                    Page oldPage = _pageDAO.FindByPK(gaim.page_id);
                    if (oldPage == null)
                    {
                        return false;
                    }
                    Page newPage = _pageDAO.FindByLanguageAndAlliasAndLink(newLanguageId,oldPage.page_alias, oldPage.page_link);
                    if (newPage == null)
                    {
                        return false;
                    }
                    GalleryImage galleryImageToInsert = new GalleryImage();
                    galleryImageToInsert.language_id = newLanguageId;
                    galleryImageToInsert.gallery_image_created = DateTime.Now;
                    galleryImageToInsert.gallery_image_modified = DateTime.Now;
                    galleryImageToInsert.gallery_image_created_by_id = gaim.gallery_image_created_by_id;
                    galleryImageToInsert.gallery_image_info = gaim.gallery_image_info;
                    galleryImageToInsert.gallery_image_link = gaim.gallery_image_link;
                    galleryImageToInsert.gallery_image_modified_by_id = gaim.gallery_image_modified_by_id;
                    galleryImageToInsert.gallery_image_ordering = gaim.gallery_image_ordering;
                    galleryImageToInsert.gallery_image_published = gaim.gallery_image_published;
                    galleryImageToInsert.page_id = newPage.page_id;
                    galleryImageToInsert.gallery_id = newGallery.gallery_id;
                    this.Add(galleryImageToInsert);
                }

            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return true;
        }

        /// <summary>
        /// Pobieranie maksymalnego numeru porządkowego elementu dla danej strony i galerii.
        /// </summary>
        /// <param name="pageId">strona</param>
        /// <param name="galleryId">galeria</param>
        /// <returns>maksymalny numer porządkowy elementu dla danego menu.</returns>
        public int? SelectMaxItemOrdering(int pageId, int galleryId)
        {
            if (pageId.Equals(0) || galleryId.Equals(0))
            {
                return 0;
            }   //if
            try
            {

                return this._cmsEntitiesDataContext.GalleryImages.Where(
                    gaim => gaim.page_id.Equals(pageId) && gaim.gallery_id.Equals(galleryId)
                        ).Max(gi => gi.gallery_image_ordering);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return 0;
        }


        /// <summary>
        /// Usuwanie listy obiektow na podstawie ich galerii i strony.
        /// </summary>
        /// <param name="galleryId">galeria</param>
        /// <param name="pageId">strona</param>
        /// <returns>true w przypadku usunięcia, false w przypadku nie odnalezienia rekordu</returns>
        public bool DeleteByGalleryAndPage(int galleryId, int pageId)
        {
            if (galleryId.Equals(0) || pageId.Equals(0))
            {
                return false;
            }   //if
            try
            {
                IQueryable<GalleryImage> gallerie_images = this._cmsEntitiesDataContext.GalleryImages.Where(
                    gall_page => gall_page.gallery_id.Equals(galleryId) && gall_page.page_id.Equals(pageId)
                        );
                if (!(gallerie_images.Any()))
                {
                    return false;
                }  
                foreach (GalleryImage i in gallerie_images)
                {
                    this._cmsEntitiesDataContext.GalleryImages.DeleteOnSubmit(i);
                }
                try
                {
                    _cmsEntitiesDataContext.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                 //if
                
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }

        /// <summary>
        /// Usuwanie listy obiektow na podstawie ich strony.
        /// </summary>
        /// <param name="pageId">strona</param>
        /// <returns>true w przypadku usunięcia, false w przypadku nie odnalezienia rekordu</returns>
        public bool DeleteByPage( int pageId)
        {
            if (pageId.Equals(0))
            {
                return false;
            }   //if
            try
            {
                IQueryable<GalleryImage> gallerie_images = this._cmsEntitiesDataContext.GalleryImages.Where(
                    page => page.page_id.Equals(pageId)
                        );
                if (!(gallerie_images.Any()))
                {
                    return false;
                }
                foreach (GalleryImage i in gallerie_images)
                {
                    this._cmsEntitiesDataContext.GalleryImages.DeleteOnSubmit(i);
                }
                try
                {
                    _cmsEntitiesDataContext.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                //if

            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }

        /// <summary>
        /// Usuwanie obiektow po jezyku.
        /// </summary>
        /// <param name="languageId">jezyk</param>
        /// <returns>true dla sukcesu, false w przeciwnym razie</returns>
        public bool DeleteByLanguage(int languageId)
        {
            if (languageId.Equals(0))
            {
                return false;
            }   //if
            try
            {
                IQueryable<GalleryImage> galleryImages = this.FindByLanguage(languageId);
                if (galleryImages == null)
                {
                    return true;
                }   //if
                if (!(galleryImages.Any()))
                {
                    return true;
                }   //if
                foreach (GalleryImage gallIm in galleryImages)
                {
                    this._cmsEntitiesDataContext.GalleryImages.DeleteOnSubmit(gallIm);
                }
                try
                {
                    _cmsEntitiesDataContext.SubmitChanges();

                }
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                return true;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }

    }
}
